package com.charactereducation.module.vo;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by zpc on 2017/2/21.
 */
public class WeChatNotification {


    /********
     * 微信反馈错误码START
     ********/

    public static final String NOAUTH = "商户无此接口权限";

    public static final String NOTENOUGH = "余额不足";

    public static final String ORDERPAID = "商户订单已支付";

    public static final String ORDERCLOSED = "订单已关闭";

    public static final String SYSTEMERROR = "系统错误";

    public static final String APPID_NOT_EXIST = "APPID不存在";

    public static final String MCHID_NOT_EXIST = "MCHID不存在";

    public static final String APPID_MCHID_NOT_MATCH = "appid和mch_id不匹配";

    public static final String LACK_PARAMS = "缺少参数";

    public static final String OUT_TRADE_NO_USED = "商户订单号重复";

    public static final String SIGNERROR = "签名错误";

    public static final String XML_FORMAT_ERROR = "XML格式错误";

    public static final String REQUIRE_POST_METHOD = "请使用post方法";

    public static final String POST_DATA_EMPTY = "post数据为空";

    public static final String NOT_UTF8 = "编码格式错误";

    /******** 微信反馈错误码END ********/





    /**
     * 获取相应的字符串
     * @param str 您传入的匹配字符串
     * @return
     */
    public synchronized static String getMessage(String str) {
        String result = null;
        switch (str) {
            case "NOAUTH":
                result = NOAUTH;
                break;
            case "NOTENOUGH":
                result = NOTENOUGH;
                break;
            case "ORDERPAID":
                result = ORDERPAID;
                break;
            case "ORDERCLOSED":
                result = ORDERCLOSED;
                break;
            case "SYSTEMERROR":
                result = SYSTEMERROR;
                break;
            case "APPID_NOT_EXIST":
                result = APPID_NOT_EXIST;
                break;
            case "MCHID_NOT_EXIST":
                result = MCHID_NOT_EXIST;
                break;
            case "APPID_MCHID_NOT_MATCH":
                result = APPID_MCHID_NOT_MATCH;
                break;
            case "LACK_PARAMS":
                result = LACK_PARAMS;
                break;
            case "OUT_TRADE_NO_USED":
                result = OUT_TRADE_NO_USED;
                break;
            case "SIGNERROR":
                result = SIGNERROR;
                break;
            case "XML_FORMAT_ERROR":
                result = XML_FORMAT_ERROR;
                break;
            case "REQUIRE_POST_METHOD":
                result = REQUIRE_POST_METHOD;
                break;
            case "POST_DATA_EMPTY":
                result = POST_DATA_EMPTY;
                break;
            case "NOT_UTF8":
                result = NOT_UTF8;
                break;
            default:
                result = "系统错误";
        }

        return result;
    }





    /**
     * 将数据库字段格式转为java属性格式
     * @param columnTemp 数据库格式的字段
     * @return java属性格式的字段
     */
    public synchronized static String convertToJava(String columnTemp) {

        if (null != columnTemp && columnTemp.length() > 0) {
            String[] columnArray = columnTemp.split("_");
            StringBuffer resultData = new StringBuffer();
            if(null != columnArray && columnArray.length > 0) {
                for(int i = 0; i < columnArray.length; i ++) {
                    String word = columnArray[i];
                    String firstChar = word.substring(0, 1);
                    String otherChars = word.substring(1);
                    String toFirstChar;
                    if(i != 0) {
                        toFirstChar = firstChar.toUpperCase();
                    } else {
                        toFirstChar = firstChar;
                    }
                    resultData.append(toFirstChar).append(otherChars);
                }
                return resultData.toString();
            }
            return "";
        }
        return "";

    }





    /**
     * 根据map的键和值进行对对象赋值
     * @param t 传入一个对象以指定兑现各类型
     * @param xmlMap 带键和值的map对象
     * @param <T> 对象类型
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public synchronized static <T> T InjectedObj(T t, Map<String, String> xmlMap, boolean isAllNew)
            throws IllegalAccessException, InstantiationException {

        Class<T> cla = (Class<T>) t.getClass();
        Object obj;
        if (isAllNew) {
            obj = cla.newInstance();
        } else {
            obj = t;
        }
        Field[] fieldArray = cla.getDeclaredFields();

        for (Field field : fieldArray) {
            field.setAccessible(true);
            Set<Map.Entry<String, String>> entrySet = xmlMap.entrySet();
            Iterator<Map.Entry<String, String>> iterator = entrySet.iterator();

            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                String elementName = entry.getKey();
                String elementValue = entry.getValue();
                String toElementName = WeChatNotification.convertToJava(elementName);

                if (toElementName.equals(field.getName())) {
                    field.set(obj, elementValue);
                    continue;
                }
            }
        }

        return (T)obj;
    }


    /**
     * 字典排序
     * @param map
     * @return
     */
    public synchronized static String dictionarySorting(Map<String, String> map) {

        if (null != map && !map.isEmpty()) {
            Collection<String> collection = map.keySet();
            List<String> list = new ArrayList<String>(collection);
            Collections.sort(list);

            StringBuilder dataString = new StringBuilder();
            for(int i = 0; i < list.size(); i ++) {
                String key = list.get(i);
                String value = map.get(key);
                if (null != map.get(key) && !map.get(key).isEmpty()) {
                    if (i == 0) {
                        dataString.append(key).append("=").append(value);
                    } else {
                        dataString.append("&").append(key).append("=").append(value);
                    }
                }
            }

            return dataString.toString();
        } else {
            return "";
        }
    }



    /** 微信反馈字典排序
    * @param map
    * @return
    */
    public synchronized static String dictionarySortingWxFeedBack(Map<String, String> map) {

        if (null != map && !map.isEmpty()) {
            Collection<String> collection = map.keySet();
            List<String> list = new ArrayList<String>(collection);
            Collections.sort(list);

            StringBuilder dataString = new StringBuilder();
            for(int i = 0; i < list.size(); i ++) {
                String key = list.get(i);
                String value = map.get(key);
                if (null != map.get(key) && !map.get(key).isEmpty()) {
                    if (!"sign".equals(key)) {
                        if (i == 0) {
                            dataString.append(key).append("=").append(value);
                        } else {
                            dataString.append("&").append(key).append("=").append(value);
                        }
                    }
                }
            }

            return dataString.toString();
        } else {
            return "";
        }
    }
}
